{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0829c265-2443-4b50-b26a-6f53d68e8933",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import glob\n",
    "import shutil\n",
    "import numpy as np\n",
    "import os.path as osp\n",
    "\n",
    "from tqdm import tqdm\n",
    "from pycocotools.coco import COCO\n",
    "\n",
    "from my_class import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "21fc9dd0-d7cd-41f0-9e59-a1808b682389",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/data/pt/data/NikeDatasets/shoe_dataset/labels\n"
     ]
    }
   ],
   "source": [
    "daily_dir = \"/data/pt/data/NikeDatasets/all/\"\n",
    "\n",
    "result_dir = \"/data/pt/data/NikeDatasets/shoe_dataset/\"\n",
    "\n",
    "result_dir_images = osp.join(result_dir, \"images\")\n",
    "result_dir_labels = osp.join(result_dir, \"labels\")\n",
    "\n",
    "if not osp.exists(result_dir_images):\n",
    "    result_dir_images = os.makedirs(osp.join(result_dir, \"images\"))\n",
    "    result_dir_labels = os.makedirs(osp.join(result_dir, \"labels\"))\n",
    "    os.makedirs(osp.join(result_dir, \"annotations\"))\n",
    "    \n",
    "print(result_dir_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "32833048-4e24-44ae-b9a5-1aa544d8e140",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nike_0326_0326\n",
      "loading annotations into memory...\n",
      "Done (t=0.00s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 88/88 [00:00<00:00, 202.65it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nike_0321_0325\n",
      "loading annotations into memory...\n",
      "Done (t=0.01s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 30%|███       | 61/200 [00:00<00:01, 138.11it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⚠️ Annotation Incorrect: nike_0321_0325 52_20250321_52_Right_Camra03_white_POC.png\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 200/200 [00:01<00:00, 130.05it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nike_0403_0407\n",
      "loading annotations into memory...\n",
      "Done (t=0.00s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 30/30 [00:00<00:00, 9134.60it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_1_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_2_Left_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_2_Left_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_2_Left_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_2_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_3_Left_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_3_Left_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_4_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_4_Left_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_4_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_4_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_4_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_5_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_5_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-1_5_Right_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_1_Left_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_1_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_1_Left_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_1_Left_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_1_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_1_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_2_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_2_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_2_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_3_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_3_Right_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_4_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_4_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_4_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0403_0407 zxc123_20250403-2_4_Right_Camra03_white_BH.png\n",
      "nike_0409_0409\n",
      "loading annotations into memory...\n",
      "Done (t=0.00s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 21/21 [00:00<00:00, 9026.48it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_1_Left_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_1_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_2_Left_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_2_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_3_Left_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_3_Left_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_3_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_3_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_4_Left_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_4_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_4_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_5_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-1_5_Left_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-2_1_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-2_1_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-2_2_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-2_2_Right_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-2_3_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-2_3_Left_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-2_3_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0409_0409 lkj0987_20250409-2_3_Right_Camra05_white_BH.png\n",
      "nike_0331_0402\n",
      "nike_0307_0324\n",
      "loading annotations into memory...\n",
      "Done (t=0.03s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 59%|█████▉    | 488/827 [00:02<00:01, 202.52it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⚠️ Annotation Incorrect: nike_0307_0324 17_20250307_17_Right_Camra01_white_BHL.png\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 827/827 [00:04<00:00, 181.49it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nike_0327_0331\n",
      "loading annotations into memory...\n",
      "Done (t=0.00s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 135/135 [00:01<00:00, 87.69it/s] \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nike_0408_0408\n",
      "loading annotations into memory...\n",
      "Done (t=0.00s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 61/61 [00:00<00:00, 8336.13it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_1_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_1_Left_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_1_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_2_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_3_Left_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_3_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_3_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_4_Right_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_5_Left_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_5_Left_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-1_5_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_1_Left_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_1_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_1_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_1_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_2_Left_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_2_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_2_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_3_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_3_Left_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_3_Right_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_3_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_3_Right_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_4_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_4_Left_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_4_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_4_Right_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_5_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_5_Left_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-2_5_Right_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_1_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_1_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_1_Left_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_1_Right_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_2_Left_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_2_Right_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_3_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_3_Left_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_3_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_3_Left_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_3_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_3_Right_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_3_Right_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 asd12346_20250408-3_3_Right_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_1_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_1_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_1_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_2_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_2_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_2_Right_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_2_Right_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_3_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_3_Left_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_3_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_3_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_3_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_4_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_4_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_4_Right_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_5_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0408_0408 mengyao123_20250408-1_5_Right_Camra04_white_BH.png\n",
      "nike_0402_0407\n",
      "loading annotations into memory...\n",
      "Done (t=0.00s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 79/79 [00:00<00:00, 6926.65it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_1_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_1_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_2_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_2_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_3_Left_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_3_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_3_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_3_Right_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_4_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_4_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_4_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_5_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_5_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_5_Right_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-1_5_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_1_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_1_Right_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_2_Left_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_2_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_3_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_3_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_3_Right_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_3_Right_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_4_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_4_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_4_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_4_Left_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_4_Right_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_4_Right_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_5_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_5_Left_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_5_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-2_5_Right_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_1_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_1_Right_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_1_Right_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_2_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_2_Left_Camra03_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_2_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_2_Left_Camra05_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_3_Left_Camra01_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_3_Left_Camra02_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_3_Left_Camra04_white_BH.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_4_Left_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_4_Left_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_4_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_5_Left_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_5_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-3_5_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_1_Left_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_1_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_1_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_1_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_1_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_2_Left_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_2_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_3_Left_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_3_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_3_Left_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_3_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_4_Left_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_4_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_4_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_4_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_5_Left_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_5_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_5_Left_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_5_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_5_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-4_5_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-5_1_Left_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-5_1_Right_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-5_2_Left_Camra03_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-5_2_Right_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-5_3_Left_Camra01_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-5_3_Right_Camra04_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-5_4_Right_Camra02_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-5_4_Right_Camra05_white_BL.png\n",
      "⚠️ Annotation Incorrect: nike_0402_0407 asd1234_20250402-7_2_Right_Camra02_white_BL.png\n",
      "nike_0325_0327\n",
      "loading annotations into memory...\n",
      "Done (t=0.00s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 129/129 [00:00<00:00, 303.48it/s]\n"
     ]
    }
   ],
   "source": [
    "for dataset_root in os.listdir(daily_dir):\n",
    "    print(dataset_root)\n",
    "    if \"nike\" not in dataset_root:\n",
    "        continue\n",
    "    if \"nike_0331_0402\" == dataset_root:\n",
    "        continue\n",
    "    rootDir = osp.join(daily_dir, dataset_root)\n",
    "    img_root_dir = osp.join(rootDir, \"images\")\n",
    "\n",
    "    i = 3\n",
    "    while i > 0:\n",
    "        anno_dir = osp.join(rootDir, f\"annotations_v{i}\")\n",
    "        if osp.exists(anno_dir):\n",
    "            break\n",
    "        i -= 1\n",
    "    if not osp.exists(anno_dir):\n",
    "        anno_dir = osp.join(rootDir, \"annotations\")\n",
    "\n",
    "    json_file = glob.glob(f\"{anno_dir}/**/*.json\", recursive=True)\n",
    "    if len(json_file) == 0:\n",
    "        continue\n",
    "    coco = COCO(json_file[0])\n",
    "    categories = coco.loadCats(coco.getCatIds())\n",
    "\n",
    "    name2id = {cat[\"name\"]:cat[\"id\"] for cat in categories}\n",
    "    if \"drunk\" not in name2id.keys():\n",
    "        print(name2id)\n",
    "        continue\n",
    "\n",
    "    for img in tqdm(coco.imgs.values()):\n",
    "        img_path = glob.glob(f\"{img_root_dir}/**/{img['file_name']}\", recursive=True)\n",
    "        if len(img_path) == 0:\n",
    "            print(f\"⚠️ Image not found: {dataset_root} {img['file_name']}\")\n",
    "            continue\n",
    "        if img['height'] == 0 or img['width'] == 0:\n",
    "            print(f\"⚠️ Image Shape Incorrect: {dataset_root} {img['file_name']}\")\n",
    "            continue\n",
    "        img_path = img_path[0]\n",
    "\n",
    "        base = coco.loadAnns(coco.getAnnIds(imgIds=img['id'], catIds=[name2id[\"drunk\"]]))\n",
    "        if len(base) == 0:\n",
    "            print(f\"⚠️ Annotation Incorrect: {dataset_root} {img['file_name']}\")\n",
    "            continue\n",
    "        x, y, w, h = base[0]['bbox']\n",
    "        # print(base)\n",
    "        img_w, img_h = img[\"width\"], img[\"height\"]\n",
    "\n",
    "        annotation = BBox.from_xywh(x, y, w, h).to_yolo_format((img_w, img_h))\n",
    "        txt = \"0 \" + \" \".join(list(map(str, annotation)))\n",
    "        if np.random.rand() < 0.5:\n",
    "            dist = osp.join(result_dir_images, \"train\")\n",
    "            os.makedirs(dist, exist_ok=True)\n",
    "            shutil.copy2(img_path, dist)            \n",
    "            dist = osp.join(result_dir_labels, \"train\")\n",
    "            os.makedirs(dist, exist_ok=True)\n",
    "            with open(osp.join(dist,  img['file_name'].replace('.png', '.txt')), \"w\") as f:\n",
    "                f.write(txt + \"\\n\")\n",
    "        else:\n",
    "            dist = osp.join(result_dir_images, \"val\")\n",
    "            os.makedirs(dist, exist_ok=True)\n",
    "            shutil.copy2(img_path, dist)            \n",
    "            dist = osp.join(result_dir_labels, \"val\")\n",
    "            os.makedirs(dist, exist_ok=True)\n",
    "            with open(osp.join(dist,  img['file_name'].replace('.png', '.txt')), \"w\") as f:\n",
    "                f.write(txt + \"\\n\")    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1242a92c-bd77-4cff-b92e-41e96b65917c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50db0f72-8885-4352-8024-92bfafd0c548",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
